{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import os\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "import time\n",
    "from PIL import Image\n",
    "import img2pdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_dir = 'input'\n",
    "output_dir = 'output'\n",
    "gamma = 2.5\n",
    "# filename_replace = 'Python源码剖析-优化_'\n",
    "filename_replace = 'Python_'\n",
    "filename_replace__v = ''\n",
    "pdf_output = f'{filename_replace}output.pdf'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def optimize(gamma: float, filename: str, log: bool=False):\n",
    "  \"\"\"伽马变换\"\"\"\n",
    "  if log:\n",
    "    print(f'process file by gamma {gamma}: {filename}')\n",
    "  image = cv2.imread(f'{input_dir}/{filename}', cv2.IMREAD_GRAYSCALE)\n",
    "  fI = image / 255.0\n",
    "  O = np.power(fI, gamma)\n",
    "  # need to extent to unit8\n",
    "  O = O * 255\n",
    "  filename = f\"{output_dir}/{filename}\"\n",
    "  cv2.imwrite(filename, O)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "renamed 0 files\n"
     ]
    }
   ],
   "source": [
    "for file in os.scandir(input_dir):\n",
    "  # rename filename with Chinese\n",
    "  count = 0\n",
    "  if file.is_file():\n",
    "    target = file.path\n",
    "    if target.find(filename_replace) > -1:\n",
    "      target = target.replace(filename_replace, filename_replace__v)\n",
    "      os.rename(file.path, target)\n",
    "      count += 1\n",
    "print(f'renamed {count} files')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time cost: 14.058048486709595s\n"
     ]
    }
   ],
   "source": [
    "b = time.time()\n",
    "pool = ThreadPoolExecutor(10)\n",
    "for file in os.scandir(input_dir):\n",
    "  if file.is_file():\n",
    "    pool.submit(optimize, gamma, file.name)\n",
    "pool.shutdown(wait=True)\n",
    "print(f'time cost: {time.time() - b}s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time cost: 5.344951629638672s\n"
     ]
    }
   ],
   "source": [
    "b = time.time()\n",
    "images = []\n",
    "for file in os.listdir(output_dir):\n",
    "  images.append(f'{output_dir}/{file}')\n",
    "images.sort(key=lambda x: int(x.replace('.jpg', '').replace('output/', '')))\n",
    "output = []\n",
    "for img in images:\n",
    "  output.append(Image.open(img))\n",
    "if len(output) > 0:\n",
    "  first_img = Image.open(images[0])\n",
    "  output.pop(0)\n",
    "  first_img.save(pdf_output, 'pdf', save_all=True, append_images=output)\n",
    "print(f'time cost: {time.time() - b}s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "c8021be2121acb6aedb321c9fc0970845e72ecff144800146fabd90eee2b1d5e"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 64-bit ('learn': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
